package com.dw.monitor.tool;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;



public class Utc {

    //将utc格式的时间转换为本地时间yyyy-MM-dd HH:mm:ss
    public static String getUtc (String utcDateStr) throws ParseException {
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf1.parse(utcDateStr.substring(0,19));//拿到Date对象
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.HOUR_OF_DAY,8);   //加8小时
        String localDateStr = sdf2.format(cal.getTime());
        return localDateStr;
    }

    //将时间戳转为yyyy-MM-dd HH:mm:ss
    public static String timeStampToDate(String time) {
        Long timeLong = Long.parseLong(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(timeLong));
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    //返回当前时间
    public static String getNow() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");//可以方便地修改日期格式
        String nowTime = dateFormat.format( now );
        return nowTime;
    }

    //返回当前时间
    public static String getNowDetail() {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String nowTime = dateFormat.format( now );
        return nowTime;
    }

    //返回当前时间
    public static String getNowDetailSub2() {

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String nowTime = dateFormat.format( now );
        return nowTime;
    }

    public static void main(String[] args) {
        String utcDateStr = "2019-08-26T06:30:33.047Z";
        System.out.println("时间"+timeStampToDate("1525849325942"));
        System.out.println(getNow());
        System.out.println(getNowDetail());
        String sysTime = timeStampToDate(String.valueOf(System.currentTimeMillis()-1000*60*60*2));
        System.out.println("SUB"+timeStampToDate(sysTime));
        System.out.println(getNowDetailSub2());
         try {
            getUtc(utcDateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
